package edu.napier.soc.xfdm.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class Database implements Serializable {

	private static final long serialVersionUID = 6685333252495674l;

	private final List<EntityType> entityTypes = new ArrayList<EntityType>();
	private final List<Constraint> constraints = new ArrayList<Constraint>();

	public boolean addEntityType(EntityType e) {
		return e != null && !entityTypes.contains(e) && entityTypes.add(e);
	}

	public void removeEntity(Entity e) {
		for (EntityType e1 : entityTypes) {
			e1.removeEntity(e);
		}
	}

	public boolean removeEntityType(EntityType e) {
		return entityTypes.remove(e);
	}

	public List<EntityType> getEntityTypes() {
		return Collections.unmodifiableList(entityTypes);
	}

	public EntityType getEntityType(String name) {
		for (EntityType e : entityTypes) {
			if (e.getName().equalsIgnoreCase(name)) {
				return e;
			}
		}
		return null;
	}

	public boolean addConstraint(Constraint c) throws ConstraintException {
		return c != null && c.getEntityType().addConstraint(c) &&
				constraints.add(c);
	}

	public Constraint getConstraint(String name) {
		for (Constraint c : constraints) {
			if (c.getName().equalsIgnoreCase(name)) {
				return c;
			}
		}
		return null;
	}

	public void clear() {
		entityTypes.clear();
		constraints.clear();
	}

	public List<Constraint> getConstraints() {
		return Collections.unmodifiableList(constraints);
	}

	public boolean removeConstraint(Constraint c) {
		return constraints.remove(c);
	}
}